﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common;
using log4net;
using SqlSugar;
using AutoFacUtils;
using CashFlow.ShopManage.Def;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Db.Repository;
using CashFlow.ShopManage.Service;

namespace CashFlow.ShopManage.Service.Implement
{
    public class FeeService : Service<FeeEntity>, IFeeService
    {
        private IFeeRepository GetFeeRepository() { return AutoFacHelper.Get<IFeeRepository>(); }
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }

        public IList<FeeAndUserInfoView> QueryByShopManagerUserId(int shopManagerUserId
            , string date
            , string name)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<FeeEntity>()
                            .InnerJoin<UserInfoEntity>((f, u) => f.UserId == u.Id)
                            .InnerJoin<ShopUserEntity>((f, u, su) => u.Id == su.UserId)
                            .Where((f, u, su) => f.Date == date
                                && su.ShopManagerUserId == shopManagerUserId);
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where((f, u, su) => u.Name.Contains(name));
                }

                var ls = query.Select((f, u, su) => new FeeAndUserInfoView
                {
                    Fee = f,
                    UserInfo = u
                }).ToList();

                return ls;
            }
        }
    }
}
